﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using HUBS1.HCMS.Data;
using HUBS1.HCMS.Entity;
using System.Data.SqlClient;

namespace HUBS1.HCMS.SqlClient
{
    public class sql_h_Plan
    {

        /// <summary>
        /// 获取计划代码信息
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        public h_Plan getItem(int ID)
        {

            DataSet ds = ExecSql.GetDataSet("select * from h_Plan where id = " + ID.ToString());

            if (ds.Tables[0].Rows.Count > 0)

                return new h_Plan(ds.Tables[0].Rows[0]);
            else
                return null;
        }


        /// <summary>
        /// 新建(覆盖已存在数据)
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public bool Add(h_Plan obj)
        {
            SqlParameter[] param = new SqlParameter[6];

            param[0] = new SqlParameter("@i_PID", obj.Pid);
            param[1] = new SqlParameter("@v_PlanCode", obj.PlanCode);
            param[2] = new SqlParameter("@v_RoomCode", obj.RoomCode);
            param[3] = new SqlParameter("@v_PlanName", obj.PlanName);
            param[4] = new SqlParameter("@v_PlanDesc", obj.PlanDesc);
            param[5] = new SqlParameter("@i_Language", obj.LanguageType);

            int i = ExecSql.SqlExecNoqueryByProcedure("sp_HCMS_Add_Plan", param);

            return true;
        }

        /// <summary>
        /// update
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public bool Update(h_Plan obj)
        {
            if(obj.ID > 0)
                return ExecSql.InsertOrUpdateData<h_Plan>(obj);

            return false;
        }

        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="strwhere"></param>
        /// <returns></returns>
        public List<h_Plan> getall(string strwhere)
        {

            string strSQL = "";

            if (strwhere != "")
                strSQL = "select * from h_plan where " + strwhere;
            else
                strSQL = "select * from h_plan ";
            try
            {
                return ExecSql.GetTableList<h_Plan>(strSQL);
            }

            catch { return null; }
        }


        /// <summary>
        /// 更改计划状态
        /// </summary>
        /// <param name="PlanCode"></param>
        /// <param name="State"></param>
        /// <returns></returns>
        public bool setStatus(int Pid, string PlanCode, string RoomCode, int State, int Language)
        {
            string strSQL = "UPDATE h_plan SET available = " + State.ToString() + 
                " WHERE property_id = @i_PID AND plancode = @v_PlanCode AND roomcode = @v_RoomCode AND languagetype = @i_Lan";

            SqlParameter[] param = new SqlParameter[4];

            param[0] = new SqlParameter("@i_PID", Pid);
            param[1] = new SqlParameter("@v_PlanCode", PlanCode);
            param[2] = new SqlParameter("@v_RoomCode", RoomCode);
            param[3] = new SqlParameter("@i_Lan", Language);

            return (ExecSql.SqlExecNoquery(strSQL, param) > 0) ? true : false;

        }

        public List<h_Plan> getListByPage(int Page, int PageSize, string strwhere, out int RowCount)
        {

            int recordcount = 0;

            //RowCount = 1;

            List<h_Plan> _log = ExecSql.GetTableList<h_Plan>("h_plan", "*", " id desc ", "id", PageSize, Page, strwhere, out recordcount);

            RowCount = recordcount;

            //pagecount = CFun.pagecount(recordcount, PageSize);

            return _log;
        }
    }
}
